package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.LabProjectWorkResearchPersonnel;
import com.ruoyi.system.mapper.LabProjectWorkResearchPersonnelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LabProjectWorkResearchMapper;
import com.ruoyi.system.domain.LabProjectWorkResearch;
import com.ruoyi.system.service.ILabProjectWorkResearchService;

/**
 * 实验室工作研究课题Service业务层处理
 * 
 * @author jun.wang
 * @date 2022-04-12
 */
@Service
public class LabProjectWorkResearchServiceImpl implements ILabProjectWorkResearchService 
{
    @Autowired
    private LabProjectWorkResearchMapper labProjectWorkResearchMapper;

    @Autowired
    private LabProjectWorkResearchPersonnelMapper labProjectWorkResearchPersonnelMapper;
    /**
     * 查询实验室工作研究课题
     * 
     * @param instanceId 实验室工作研究课题ID
     * @return 实验室工作研究课题
     */
    @Override
    public LabProjectWorkResearch selectLabProjectWorkResearchById(Long instanceId)
    {
        LabProjectWorkResearch labProjectWorkResearch = labProjectWorkResearchMapper.selectLabProjectWorkResearchById(instanceId);
        // 查询工作研究参与人员
        List<LabProjectWorkResearchPersonnel> labProjectWorkResearchPersonnels = labProjectWorkResearchPersonnelMapper.selectLabProjectWorkResearchByWorkResearchId(instanceId);
        if (null == labProjectWorkResearchPersonnels){
            labProjectWorkResearchPersonnels = new ArrayList<>();
        }
        labProjectWorkResearch.setLabProjectWorkResearchPersonnels(labProjectWorkResearchPersonnels);
        return labProjectWorkResearch;
    }

    /**
     * 查询实验室工作研究课题列表
     * 
     * @param labProjectWorkResearch 实验室工作研究课题
     * @return 实验室工作研究课题
     */
    @Override
    public List<LabProjectWorkResearch> selectLabProjectWorkResearchList(LabProjectWorkResearch labProjectWorkResearch)
    {
        return labProjectWorkResearchMapper.selectLabProjectWorkResearchList(labProjectWorkResearch);
    }

    /**
     * 新增实验室工作研究课题
     * 
     * @param labProjectWorkResearch 实验室工作研究课题
     * @return 结果
     */
    @Override
    public int insertLabProjectWorkResearch(LabProjectWorkResearch labProjectWorkResearch)
    {
        int i = labProjectWorkResearchMapper.insertLabProjectWorkResearch(labProjectWorkResearch);
        // 保存参与人员
        List<LabProjectWorkResearchPersonnel> labProjectWorkResearchPersonnels = labProjectWorkResearch.getLabProjectWorkResearchPersonnels();
        for (LabProjectWorkResearchPersonnel labProjectWorkResearchPersonnel : labProjectWorkResearchPersonnels) {
            labProjectWorkResearchPersonnel.setWorkResearchId(labProjectWorkResearch.getInstanceId());
            labProjectWorkResearchPersonnelMapper.insertLabProjectWorkResearchPersonnel(labProjectWorkResearchPersonnel);
        }
        return i;
    }

    /**
     * 修改实验室工作研究课题
     * 
     * @param labProjectWorkResearch 实验室工作研究课题
     * @return 结果
     */
    @Override
    public int updateLabProjectWorkResearch(LabProjectWorkResearch labProjectWorkResearch)
    {
        int i = labProjectWorkResearchMapper.updateLabProjectWorkResearch(labProjectWorkResearch);
        // 删除参与人员
        labProjectWorkResearchPersonnelMapper.deleteLabProjectWorkResearchPersonnelByWorkResearchId(labProjectWorkResearch.getInstanceId());
        // 保存参与人员
        List<LabProjectWorkResearchPersonnel> labProjectWorkResearchPersonnels = labProjectWorkResearch.getLabProjectWorkResearchPersonnels();
        for (LabProjectWorkResearchPersonnel labProjectWorkResearchPersonnel : labProjectWorkResearchPersonnels) {
            labProjectWorkResearchPersonnel.setWorkResearchId(labProjectWorkResearch.getInstanceId());
            labProjectWorkResearchPersonnelMapper.insertLabProjectWorkResearchPersonnel(labProjectWorkResearchPersonnel);
        }
        return i;
    }

    /**
     * 批量删除实验室工作研究课题
     * 
     * @param instanceIds 需要删除的实验室工作研究课题ID
     * @return 结果
     */
    @Override
    public int deleteLabProjectWorkResearchByIds(Long[] instanceIds)
    {
        // 删除参与人员
        for (Long instanceId : instanceIds) {
            labProjectWorkResearchPersonnelMapper.deleteLabProjectWorkResearchPersonnelByWorkResearchId(instanceId);
        }
        return labProjectWorkResearchMapper.deleteLabProjectWorkResearchByIds(instanceIds);
    }

    /**
     * 删除实验室工作研究课题信息
     * 
     * @param instanceId 实验室工作研究课题ID
     * @return 结果
     */
    @Override
    public int deleteLabProjectWorkResearchById(Long instanceId)
    {
        // 删除参与人员

        labProjectWorkResearchPersonnelMapper.deleteLabProjectWorkResearchPersonnelByWorkResearchId(instanceId);
        return labProjectWorkResearchMapper.deleteLabProjectWorkResearchById(instanceId);
    }
}
